{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "d00fc083",
   "metadata": {
    "papermill": {
     "duration": 0.002622,
     "end_time": "2022-10-26T08:27:02.102770",
     "exception": false,
     "start_time": "2022-10-26T08:27:02.100148",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "# output-elastic"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "645dc2c4",
   "metadata": {
    "papermill": {
     "duration": 0.004143,
     "end_time": "2022-10-26T08:27:02.111011",
     "exception": false,
     "start_time": "2022-10-26T08:27:02.106868",
     "status": "completed"
    },
    "tags": []
   },
   "source": [
    "This component creates a container image for dumping data into elastic search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4e59fe38",
   "metadata": {
    "papermill": {
     "duration": 2.308259,
     "end_time": "2022-10-26T08:27:04.423791",
     "exception": false,
     "start_time": "2022-10-26T08:27:02.115532",
     "status": "completed"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "import logging\n",
    "import os\n",
    "import sys\n",
    "import re\n",
    "\n",
    "#os.environ['create_image']='True'\n",
    "os.environ['repository']='docker.io/romeokienzler'\n",
    "os.environ['version']='0.5'\n",
    "os.environ['name']='claimed-output-elastic'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dde74e6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sending build context to Docker daemon  30.21kB\n",
      "Step 1/4 : FROM debian\n",
      " ---> 54e726b437fb\n",
      "Step 2/4 : RUN apt update && apt install -y python3-pip\n",
      " ---> Using cache\n",
      " ---> 88d22e8c84f5\n",
      "Step 3/4 : RUN pip install ipython nbformat elasticsearch==7.9.1\n",
      " ---> Using cache\n",
      " ---> 0973aeff9f48\n",
      "Step 4/4 : ADD output-elastic.ipynb .\n",
      " ---> d448c2070759\n",
      "Successfully built d448c2070759\n",
      "Successfully tagged claimed-output-elastic:0.5\n",
      "The push refers to repository [docker.io/romeokienzler/claimed-output-elastic]\n",
      "\n",
      "\u001b[1B29fceabb: Preparing \n",
      "\u001b[1Ba2c4fdc8: Preparing \n",
      "\u001b[1Bc0e285b8: Preparing \n",
      "\u001b[4B29fceabb: Pushed lready exists kB\u001b[1A\u001b[2K\u001b[3A\u001b[2K\u001b[2A\u001b[2K\u001b[4A\u001b[2K0.5: digest: sha256:7af95678a0136712ec5e4f066b94324f3d9a318f4e6a82a030841034445b9476 size: 1162\n"
     ]
    }
   ],
   "source": [
    "if bool(os.environ.get('create_image',False)):\n",
    "    docker_file=\"\"\"\n",
    "    FROM debian\n",
    "    RUN apt update && apt install -y python3-pip\n",
    "    RUN pip install ipython nbformat elasticsearch==7.9.1\n",
    "    ADD output-elastic.ipynb .\n",
    "    \"\"\"\n",
    "    with open(\"Dockerfile\", \"w\") as text_file:\n",
    "        text_file.write(docker_file)\n",
    "\n",
    "    !docker build -t `echo $name`:`echo $version` .\n",
    "    !docker tag `echo $name`:`echo $version` `echo $repository`/`echo $name`:`echo $version`\n",
    "    !docker push `echo $repository`/`echo $name`:`echo $version`\n",
    "    !rm Dockerfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6cd66f9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "from elasticsearch import Elasticsearch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c79bea26",
   "metadata": {},
   "outputs": [],
   "source": [
    "# connection string\n",
    "connection_string = os.environ.get('connection_string')\n",
    "\n",
    "#  document to store\n",
    "document = os.environ.get('document')\n",
    "\n",
    "# index\n",
    "index = os.environ.get('index')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e06c3dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "parameters = list(\n",
    "    map(lambda s: re.sub('$', '\"', s),\n",
    "        map(\n",
    "            lambda s: s.replace('=', '=\"'),\n",
    "            filter(\n",
    "                lambda s: s.find('=') > -1 and bool(re.match(r'[A-Za-z0-9_]*=[.\\/A-Za-z0-9]*', s)),\n",
    "                sys.argv\n",
    "            )\n",
    "    )))\n",
    "\n",
    "for parameter in parameters:\n",
    "    logging.debug('Parameter: ' + parameter)\n",
    "    exec(parameter)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "50728e11",
   "metadata": {},
   "outputs": [],
   "source": [
    "es = Elasticsearch([connection_string], verify_certs=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0afed6d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "es.index(index=index, body=document)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.1"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 5.223558,
   "end_time": "2022-10-26T08:27:05.697642",
   "environment_variables": {},
   "exception": null,
   "input_path": "/home/romeokienzler/gitco/claimed/component-library/input/input-url.ipynb",
   "output_path": "/home/romeokienzler/gitco/claimed/component-library/input/input-url.ipynb",
   "parameters": {},
   "start_time": "2022-10-26T08:27:00.474084",
   "version": "2.3.3"
  },
  "vscode": {
   "interpreter": {
    "hash": "e7370f93d1d0cde622a1f8e1c04877d8463912d04d973331ad4851f04de6915a"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
